// 该组件为通用表格编辑组件，被多个页面复用，已归类到 common 目录
<template>
  <el-form ref="formRef" :model="form" :rules="rules" :label-width="labelWidth">
    <el-row>
      <el-col v-for="item in columns" :key="item.prop" :span="item.span || span">
        <el-form-item :label="item.label" :hidden="!!item.hidden" :prop="item.prop">
          <!-- 文本框、数字框、下拉框、日期框、开关、上传 -->
          <el-input
            v-if="item.type === 'input'"
            v-model="form[item.prop]"
            :disabled="item.disabled"
            :placeholder="item.placeholder"
            clearable
          ></el-input>
          <el-input
            v-if="item.type === 'textarea'"
            v-model="form[item.prop]"
            type="textarea"
            :disabled="item.disabled"
            :placeholder="item.placeholder"
            clearable
          ></el-input>
          <el-input-number
            v-else-if="item.type === 'number'"
            v-model="form[item.prop]"
            :disabled="item.disabled"
            controls-position="right"
          ></el-input-number>
          <el-select
            v-else-if="item.type === 'select'"
            v-model="form[item.prop]"
            :disabled="item.disabled"
            :placeholder="item.placeholder"
            clearable
          >
            <el-option
              v-for="opt in item.opts"
              :key="opt.value"
              :label="opt.label"
              :value="opt.value"
            ></el-option>
          </el-select>
          <el-date-picker
            v-else-if="item.type === 'date'"
            v-model="form[item.prop]"
            type="date"
            :value-format="item.format"
          ></el-date-picker>
          <el-switch
            v-else-if="item.type === 'switch'"
            v-model="form[item.prop]"
            :active-value="item.activeValue"
            :inactive-value="item.inactiveValue"
            :active-text="item.activeText"
            :inactive-text="item.inactiveText"
          ></el-switch>
          <el-upload
            v-else-if="item.type === 'upload'"
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="form[item.prop]" :src="form[item.prop]" class="avatar" alt="" />
            <el-icon v-else class="avatar-uploader-icon">
              <Plus />
            </el-icon>
          </el-upload>
          <slot v-else :name="item.type" :rows="item" :row="form"></slot>
        </el-form-item>
      </el-col>
    </el-row>

    <el-form-item>
      <el-button type="primary" @click="saveEdit(formRef)">保 存</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
  import { FormOption } from '@/types/form-option'
  import { FormInstance, FormRules, UploadProps } from 'element-plus'
  import { PropType, ref } from 'vue'
  import { Plus } from '@element-plus/icons-vue'
  import { submitForm } from '@/utils/validate.ts'

  const { span, labelWidth, columns, formData, edit, update } = defineProps({
    span: {
      type: Number,
      required: true,
    },
    labelWidth: {
      type: String,
      required: true,
    },
    columns: {
      type: Array as PropType<FormOption[]>,
      required: true,
    },
    formData: {
      type: Object,
      required: true,
    },
    edit: {
      type: Boolean,
      required: false,
    },
    update: {
      type: Function,
      required: true,
    },
  })

  const form = ref({ ...(edit ? formData : {}) })

  const rules: FormRules = columns
    .map(item => {
      if (item.required) {
        return {
          [item.prop]: [{ required: true, message: `${item.label}不能为空`, trigger: 'blur' }],
        }
      }
      return {}
    })
    .reduce((acc, cur) => ({ ...acc, ...cur }), {})

  const formRef = ref<FormInstance>()

  const saveEdit = (formEl: FormInstance | undefined) => {
    submitForm(formEl, update, form.value)
  }

  const handleAvatarSuccess: UploadProps['onSuccess'] = (_response, uploadFile) => {
    form.value.thumb = URL.createObjectURL(uploadFile.raw!)
  }
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
  }

  .avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
  }

  .el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
  }
</style>
